package com.wx.dao.impl;

import com.wx.dao.InDao;
import com.wx.domain.In;
import com.wx.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author 27250
 */
public class InDaoImpl implements InDao {

    private JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public void addDrugNumber(String drugId, String doctorId, int inNumber, String inDate) {
        jdbcTemplate.update("insert into `in` values(null,?,?,?,?)",drugId,inNumber,doctorId,inDate);
    }

    @Override
    public int getCount() {
        return jdbcTemplate.queryForObject("select count(*) from `in`", int.class);
    }

    @Override
    public List<In> getInList(String pageIndex, String pageSize) {
        return jdbcTemplate.query("select * from `in` order by inId limit ? ,?",
                new BeanPropertyRowMapper<>(In.class),Integer.parseInt(pageIndex)-1,Integer.parseInt(pageSize));
    }

    @Override
    public int getCountByDrugId(String searchData) {
        return jdbcTemplate.queryForObject("select count(*) from `in` where drugId = ?", int.class,searchData);
    }

    @Override
    public List<In> getInListByDrugId(String searchData, int pageIndex, int pageSize) {
        return jdbcTemplate.query("select * from `in` where drugId = ? order by inId limit ? ,?",
                new BeanPropertyRowMapper<>(In.class),searchData,pageIndex-1,pageSize);
    }

    @Override
    public int getCountByDoctorId(String searchData) {
        return jdbcTemplate.queryForObject("select count(*) from `in` where inDoctor = ?", int.class,searchData);
    }

    @Override
    public List<In> getInListByDoctorId(String searchData, int pageIndex, int pageSize) {
        return jdbcTemplate.query("select * from `in` where inDoctor = ? order by inId limit ? ,?",
                new BeanPropertyRowMapper<>(In.class),searchData,pageIndex-1,pageSize);
    }
}
